package com.okilo.online.app.tools.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.google.android.gms.auth.api.phone.SmsRetriever
import com.google.android.gms.common.api.CommonStatusCodes
import com.google.android.gms.common.api.Status

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/11/28 21:38
 * @Annotation：SMS 验证码广播接收器
 */
class OkiloVerifyMobileBroadReceiver(val result: (Intent) -> Unit): BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras?.get(SmsRetriever.EXTRA_STATUS) as? Status
            val code = status?.statusCode ?: -1
            if (code == CommonStatusCodes.SUCCESS) {
                //获取内容Intent，只能通过 startActivityForResult(intent) 才能读取短信内容
                val consentIntent = extras?.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
                consentIntent?.let {
                    result.invoke(it)
                }
            }
        }
    }
}